package com.zwl.sjzmanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwl.execption.ZWLException;
import com.zwl.model.Atlas;
import com.zwl.model.RoomCard;
import com.zwl.model.RoomContainers;
import com.zwl.sjzmanagement.mapper.RoomCardMapper;
import com.zwl.sjzmanagement.service.RoomCardService;
import com.zwl.vo.RoomCardContainersVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipException;

@Service
public class RoomCardServiceImpl extends ServiceImpl<RoomCardMapper, RoomCardContainersVo> implements RoomCardService {

    @Autowired
    private RoomCardMapper roomCardMapper;

    @Override
    public Map<String, List<RoomCardContainersVo>> roomCardList() {

        HashMap<String, List<RoomCardContainersVo>> resultMap = new HashMap<>();
        List<Atlas> atlas = roomCardMapper.queryMapList();

        for (Atlas atla : atlas) {
            String altaName = atla.getMapName();
            List<RoomCard> roomCardsOfOneMap = roomCardMapper.queryRoomCardList(altaName);

            List<RoomCardContainersVo> collect = roomCardsOfOneMap.stream().map(roomCard -> {
                RoomCardContainersVo roomCardContainersVo = new RoomCardContainersVo();
                BeanUtils.copyProperties(roomCard, roomCardContainersVo);
                List<RoomContainers> roomContainers = roomCardMapper.queryRoomToContainers(roomCard.getRoomName());
                roomCardContainersVo.setRoomContainers(roomContainers);
                return roomCardContainersVo;
            }).collect(Collectors.toList());

            resultMap.put(altaName, collect);
        }

        return resultMap;




    }

    @Override
    public void roomCardAddOrUpdate(RoomCardContainersVo roomCardContainersVo) {

        if (roomCardContainersVo.getRoomContainers() == null) {
            throw new ZWLException(201,"无数据传递");
        }
        List<Atlas> atlases = roomCardMapper.queryMapList();
        List<String> mapNames = atlases.stream().map(Atlas::getMapName).toList();

        if (!mapNames.contains(roomCardContainersVo.getMapName())){
            throw new ZWLException(201,"添加的房卡所在地图不属于已有地图");
        }
        if (roomCardContainersVo.getId()!=null && roomCardMapper.queryById(roomCardContainersVo.getId())!=null) {
            //更新
            RoomCard roomCard = new RoomCard();
            BeanUtils.copyProperties(roomCardContainersVo, roomCard);
            roomCardMapper.updateRoomCard(roomCard);

            RoomContainers newRoomContainers = new RoomContainers();
            for (RoomContainers roomContainers : roomCardContainersVo.getRoomContainers()) {
                BeanUtils.copyProperties(roomContainers, newRoomContainers);
                roomCardMapper.updateRoomContainers(newRoomContainers);
            }

        }else {
            //新增
            RoomCard roomCard = new RoomCard();
            BeanUtils.copyProperties(roomCardContainersVo, roomCard);
            roomCardMapper.addRoomCard(roomCard);

            RoomContainers newRoomContainers = new RoomContainers();
            for (RoomContainers roomContainers : roomCardContainersVo.getRoomContainers()) {
                BeanUtils.copyProperties(roomContainers, newRoomContainers);
                roomCardMapper.addRoomContainers(newRoomContainers);
            }
        }

    }

    @Override
    public void deleteRCById(Integer id) {
        try {
            RoomCard roomCard = roomCardMapper.queryById(id);
            String roomName = roomCard.getRoomName();
            roomCardMapper.deleteRoomCardById(id);


            roomCardMapper.deleteRoomContainersByName(roomName);
        } catch (Exception e){
            throw new ZWLException(201,"rc删除失败");
        }
    }

    @Override
    public void BatchdeleteRCById(List<Integer> ids) {

        for (Integer id : ids) {
            RoomCard roomCard = roomCardMapper.queryById(id);
            String roomName = roomCard.getRoomName();
            roomCardMapper.deleteRoomCardById(id);


            roomCardMapper.deleteRoomContainersByName(roomName);
        }
    }

    @Override
    public RoomCardContainersVo getRoomCardVoById(Integer id) {

        RoomCard roomCard = roomCardMapper.queryById(id );
        RoomCardContainersVo roomCardContainersVo = new RoomCardContainersVo();
        BeanUtils.copyProperties(roomCard, roomCardContainersVo);
        List<RoomContainers> roomContainers = roomCardMapper.queryRoomToContainers(roomCard.getRoomName());
        roomCardContainersVo.setRoomContainers(roomContainers);
        return roomCardContainersVo;
    }
}






















